import { defHttp } from "/@/utils/http/axios"
import { Modal } from "ant-design-vue"

enum Api {
  list = "/misc-service/shortLink/pageLink",
  save = "/misc-service/shortLink/saveLink",
  get = "/misc-service/shortLink/getLink",
  edit = "/misc-service/shortLink/updateLink",
  deleteBatch = "/misc-service/shortLink/batchDeleteLink",

  saveGroup = "/misc-service/shortLink/saveGroup",
  updateGroup = "/misc-service/shortLink/updateGroup",
  batchDeleteGroup = "/misc-service/shortLink/batchDeleteGroup",
  deleteGroup = "/misc-service/shortLink/deleteGroup",
}

export const listLink = (params) => defHttp.get({ url: Api.list, params })
export const getLink = (params) => defHttp.get({ url: Api.get, params })
export const saveOrUpdateLink = (params, isUpdate) => defHttp.post({ url: isUpdate ? Api.edit : Api.save, params })
export const saveOrUpdateGroup = (params, isUpdate) => defHttp.post({ url: isUpdate ? Api.updateGroup : Api.saveGroup, params })

/**
 * 删除
 * @param params
 */
export const deleteLink = (params, handleSuccess) => {
  return defHttp
    .delete(
      {
        url: Api.deleteBatch,
        data: {
          ids: [params.id],
        },
      },
      { joinParamsToUrl: true }
    )
    .then(() => {
      handleSuccess()
    })
}

/**
 * 批量删除
 * @param params
 */
export const batchDeleteLink = (params, handleSuccess) => {
  Modal.confirm({
    title: "确认删除",
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}

export const deleteGroup = (params, handleSuccess) => {
  Modal.confirm({
    title: `确认删除${params.name}`,
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.deleteGroup, data: { id: params.id } }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}

export const batchDeleteGroup = (params, handleSuccess) => {
  Modal.confirm({
    title: `确认删除`,
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.batchDeleteGroup, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}
